import axios from 'axios';
import router from "@/router";
import { ElMessage } from 'element-plus'
import { getToken } from './auth'
const api = axios.create({
    baseURL: '/api',
})

api.interceptors.request.use(
    (config) => {
        const token = getToken()
        if (token) {
            config.headers.token = token
        }
        return config
    },
    (error) => {
        return Promise.reject(error)
    }
);
api.interceptors.response.use(
    (response) => {
        if (response.data.code === 410) {
            ElMessage.error('登录已过期，请重新登录');
            router.push('/login');
        }
        return response.data;
    },
    (error) => {
        if (error.response.data.code === 410) {
            ElMessage.error(error.response.data.message);
            router.push('/login');
        }
        return Promise.reject(error);
    }
);
export default api;